import 'dart:async';

import 'package:flutter/services.dart';

class HprtCpcl {
  static const MethodChannel _channel = MethodChannel('hprt_cpcl');

  /// 连接打印机
  static Future<int?> portOpenBT(String portSetting) async {
    final int? res =
        await _channel.invokeMethod('portOpenBT', {'portSetting': portSetting});
    return res;
  }

  /// 断开连接
  static Future<int?> portClose() async {
    final int? res = await _channel.invokeMethod('portClose');
    return res;
  }

  /// 获取配对的蓝牙设备
  static Future<dynamic> getDevices() async {
    final dynamic res = await _channel.invokeMethod('devices');
    return res;
  }

  /// 设置标签高度
  static Future<int?> printAreaSize(
      {required String offset,
      required String horizontal,
      required String vertical,
      required String height,
      String qty = '0'}) async {
    final int? res = await _channel.invokeMethod('printAreaSize', {
      'offset': offset,
      'horizontal': horizontal,
      'vertical': vertical,
      'height': height,
      'qty': qty
    });
    return res;
  }

  /// 打印
  static Future<int?> print() async {
    final int? res = await _channel.invokeMethod('print');
    return res;
  }

  /// 设置编码 gb2312 中文
  static Future<int?> encoding({String code = 'gb2312'}) async {
    final int? res = await _channel.invokeMethod('encoding', {'code': code});
    return res;
  }

  /// 标签定位
  static Future<int?> form() async {
    final int? res = await _channel.invokeMethod('form');
    return res;
  }

  /// 注释
  static Future<int?> note(String note) async {
    final int? res = await _channel.invokeMethod('note', {'note': note});
    return res;
  }

  /// 终止指令
  static Future<int?> abort() async {
    final int? res = await _channel.invokeMethod('abort');
    return res;
  }

  /// 文本打印
  static Future<int?> text(
      {String command = 'TEXT',
      String font = '0',
      required String x,
      required String y,
      required String data}) async {
    final int? res = await _channel.invokeMethod('text',
        {'command': command, 'font': font, 'x': x, 'y': y, 'data': data});
    return res;
  }

  /// 文本打印 用于中文固件
  static Future<int?> printTextCPCL(
      {String command = 'TEXT',
      int font = 16,
      required String x,
      required String y,
      required String data,
      int n = 0,
      bool isCenter = false,
      int width = 0}) async {
    final int? res = await _channel.invokeMethod('printTextCPCL', {
      'command': command,
      'font': font,
      'x': x,
      'y': y,
      'data': data,
      'n': n,
      'isCenter': isCenter,
      'width': width
    });
    return res;
  }

  /// 文本打印 用于英文固件
  static Future<int?> printCodepageTextCPCL(
      {String command = 'TEXT',
      int font = 0,
      required String x,
      required String y,
      required String data,
      int n = 0}) async {
    final int? res = await _channel.invokeMethod('text', {
      'command': command,
      'font': font,
      'x': x,
      'y': y,
      'data': data,
      'n': n,
    });
    return res;
  }

  /// 计数
  static Future<int?> count(String ml) async {
    final int? res = await _channel.invokeMethod('count', {'ml': ml});
    return res;
  }

  /// 设置字符宽高放大倍数
  static Future<int?> setMag(
      {required String width, required String height}) async {
    final int? res = await _channel
        .invokeMethod('setMag', {'width': width, 'height': height});
    return res;
  }

  /// 对齐方式
  static Future<int?> align(String align) async {
    final int? res = await _channel.invokeMethod('align', {'align': align});
    return res;
  }

  /// 打印条码
  static Future<int?> barcode(
      {String command = 'BARCODE',
      required String width,
      String ratio = '0',
      required String height,
      required String x,
      required String y,
      bool undertext = false,
      String number = '',
      String size = '',
      String offset = '',
      required String data}) async {
    final int? res = await _channel.invokeMethod('barcode', {
      'command': command,
      'width': width,
      'ratio': ratio,
      'height': height,
      'x': x,
      'y': y,
      'undertext': undertext,
      'number': number,
      'size': size,
      'offset': offset,
      'data': data
    });
    return res;
  }

  /// 打印二维码
  static Future<int?> printQR(
      {String command = 'BARCODE',
      required String x,
      required String y,
      String m = '2',
      required String u,
      required String data}) async {
    final int? res = await _channel.invokeMethod('printQR', {
      'command': command,
      'x': x,
      'y': y,
      'm': m,
      'u': u,
      'data': data,
    });
    return res;
  }

  /// 打印矩形框
  static Future<int?> box(
      {required String x0,
      required String y0,
      required String x1,
      required String y1,
      String width = '1'}) async {
    final int? res = await _channel.invokeMethod(
        'box', {'x0': x0, 'y0': y0, 'x1': x1, 'y1': y1, 'width': width});
    return res;
  }

  /// 打印直线
  static Future<int?> line(
      {required String x0,
      required String y0,
      required String x1,
      required String y1,
      String width = '1'}) async {
    final int? res = await _channel.invokeMethod('line', {
      'x0': x0,
      'y0': y0,
      'x1': x1,
      'y1': y1,
      'width': width,
    });
    return res;
  }

  /// 反白框
  static Future<int?> inverseLine(
      {required String x0,
      required String y0,
      required String x1,
      required String y1,
      String width = '1'}) async {
    final int? res = await _channel.invokeMethod('inverseLine', {
      'x0': x0,
      'y0': y0,
      'x1': x1,
      'y1': y1,
      'width': width,
    });
    return res;
  }

  /// 打印图片
  static Future<int?> printBitmapCPCL(
      {required List<int> bytes,
      required int x,
      required int y,
      int type = 0,
      int compressType = 0,
      int light = 0}) async {
    final int? res = await _channel.invokeMethod('printBitmapCPCL', {
      'bytes': bytes,
      'x': x,
      'y': y,
      'type': type,
      'compressType': compressType,
      'light': light,
    });
    return res;
  }

  /// 打印图片
  static Future<int?> printBitmap(
      {required int x,
      required int y,
      int type = 0,
      required List<int> bytes,
      int width = 0,
      int height = 0,
      int rotate = 0,
      int compressType = 0,
      bool isform = false,
      int segments = 1}) async {
    final int? res = await _channel.invokeMethod('printBitmap', {
      'bytes': bytes,
      'x': x,
      'y': y,
      'type': type,
      'compressType': compressType,
      'isform': isform,
      'segments': segments,
      'width': width,
      'height': height,
      'rotate': rotate
    });
    return res;
  }

  /// 打印浓度
  static Future<int?> contrast(String contrast) async {
    final int? res =
        await _channel.invokeMethod('contrast', {'contrast': contrast});
    return res;
  }

  /// 打印速度
  static Future<int?> speed(String speed) async {
    final int? res = await _channel.invokeMethod('speed', {'speed': speed});
    return res;
  }

  /// 设置行间距
  static Future<int?> setSp(String setsp) async {
    final int? res = await _channel.invokeMethod('setSp', {'setsp': setsp});
    return res;
  }

  /// 走纸
  static Future<int?> preFeed(String prefeed) async {
    final int? res =
        await _channel.invokeMethod('preFeed', {'prefeed': prefeed});
    return res;
  }

  /// 打印完走纸
  static Future<int?> postFeed(String posfeed) async {
    final int? res =
        await _channel.invokeMethod('postFeed', {'posfeed': posfeed});
    return res;
  }

  /// 蜂鸣器
  static Future<int?> beep(String beep) async {
    final int? res = await _channel.invokeMethod('beep', {'beep': beep});
    return res;
  }

  /// 下划线
  static Future<int?> underline(bool ul) async {
    final int? res = await _channel.invokeMethod('underline', {'ul': ul});
    return res;
  }

  /// 延时打印
  static Future<int?> wait(String wait) async {
    final int? res = await _channel.invokeMethod('wait', {'wait': wait});
    return res;
  }

  /// 行模式下设置行间距
  static Future<int?> setlf(String sf) async {
    final int? res = await _channel.invokeMethod('setlf', {'sf': sf});
    return res;
  }

  /// 打印宽度
  static Future<int?> pageWidth(String pw) async {
    final int? res = await _channel.invokeMethod('pageWidth', {'pw': pw});
    return res;
  }

  /// 行模式字体大小与行高
  static Future<int?> setlp(
      {required String font,
      required String size,
      required String spacing}) async {
    final int? res = await _channel.invokeMethod('setlp', {
      'font': font,
      'size': size,
      'spacing': spacing,
    });
    return res;
  }

  /// 发送数据
  static Future<int?> writeData(bData) async {
    final int? res = await _channel.invokeMethod('writeData', {'bData': bData});
    return res;
  }

  /// 字体加粗
  static Future<int?> setBold(String bold) async {
    final int? res = await _channel.invokeMethod('setBold', {'bold': bold});
    return res;
  }

  /// 获取打印机状态
  static Future<int?> getStatus() async {
    final int? res = await _channel.invokeMethod('getStatus');
    return res;
  }

  /// 文本打印自动换行
  /// 该功能有两个接⼝分别是AutLine和AutLine2。前者不能使⽤泰语。后者打印机固件必须在A300V1.01.40.01以上且⽂本字节数不得超过1024，超过部分⾃动忽略
  static Future<int?> autLine(
      {required String x,
      required String y,
      required int width,
      int size = 8,
      bool isbole = false,
      bool isdouble = false,
      required String str}) async {
    final int? res = await _channel.invokeMethod('autLine', {
      'x': x,
      'y': y,
      'width': width,
      'size': size,
      'isbole': isbole,
      'isdouble': isdouble,
      'str': str
    });
    return res;
  }

  /// 文本打印自动换行
  static Future<int?> autLine2(
      {required String x,
      required String y,
      required int width,
      int size = 8,
      bool isbole = false,
      bool isdouble = false,
      required String str}) async {
    final int? res = await _channel.invokeMethod('autLine2', {
      'x': x,
      'y': y,
      'width': width,
      'size': size,
      'isbole': isbole,
      'isdouble': isdouble,
      'str': str
    });
    return res;
  }

  /// 文本在文本框内居中显示
  static Future<int?> autCenter(
      {String command = 'TEXT',
      required String x,
      required String y,
      required int width,
      int size = 8,
      required String str}) async {
    final int? res = await _channel.invokeMethod('autCenter', {
      'command': command,
      'x': x,
      'y': y,
      'width': width,
      'size': size,
      'str': str,
    });
    return res;
  }

  /// 设置打印机纸张类型 只⽀持 A300
  static Future<int?> paperTypeCPCL(int page) async {
    final int? res =
        await _channel.invokeMethod('paperTypeCPCL', {'page': page});
    return res;
  }

  /// 自检页
  static Future<int?> setSelf() async {
    final int? res = await _channel.invokeMethod('setSelf');
    return res;
  }

  /// 旋转180度打印
  static Future<int?> poPrint() async {
    final int? res = await _channel.invokeMethod('poPrint');
    return res;
  }

  /// 打印完成的开关
  static Future<int?> openEndStatic(bool isopen) async {
    final int? res =
        await _channel.invokeMethod('getEndStatus', {'isopen': isopen});
    return res;
  }

  /// 获取打印完成时状态
  static Future<int?> getEndStatus(int time) async {
    final int? res =
        await _channel.invokeMethod('getEndStatus', {'time': time});
    return res;
  }

  /// 打印机回退
  static Future<int?> reverseFeed(int feed) async {
    final int? res = await _channel.invokeMethod('reverseFeed', {'feed': feed});
    return res;
  }

  /// 水印
  static Future<int?> printBackground(
      {required int x,
      required int y,
      required int size,
      required int background,
      required String data}) async {
    final int? res = await _channel.invokeMethod('printBackground', {
      'x': x,
      'y': y,
      'background': background,
      'size': size,
      'data': data,
    });
    return res;
  }

  /// 设置打印机Codepage
  static Future<int?> country(String codepage) async {
    final int? res =
        await _channel.invokeMethod('country', {'codepage': codepage});
    return res;
  }

  /// 设置QRcode版本
  static Future<int?> setQRcodeVersion(int version) async {
    final int? res =
        await _channel.invokeMethod('setQRcodeVersion', {'version': version});
    return res;
  }

  /// 行模式打印文本 以\r\n结尾
  static Future<int?> printData(String str) async {
    final int? res = await _channel.invokeMethod('printData', {'str': str});
    return res;
  }

  /// 行模式字体加粗
  static Future<int?> rowSetBold(String bold) async {
    final int? res = await _channel.invokeMethod('rowSetBold', {'bold': bold});
    return res;
  }

  /// 行模式X坐标 必须放在setlp之前
  static Future<int?> rowSetX(String x) async {
    final int? res = await _channel.invokeMethod('rowSetX', {'x': x});
    return res;
  }

  static Future<int?> printQrCodeImage(
      {required String data,
      required int width,
      required int height,
      required int x,
      required int y,
      int type = 0,
      int compressType = 0,
      int light = 0}) async {
    final int? res = await _channel.invokeMethod('printQRCodeImage', {
      'data': data,
      'width': width,
      'height': height,
      'x': x,
      'y': y,
      'type': type,
      'compressType': compressType,
      'light': light,
    });
    return res;
  }
}
